﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogicTree.Interfaces;

namespace LogicTree
{
    public class CalculateError
    {
        public CalculateError(Disjunction<bool> disjunct, LogicSet data)
        {
            _disjunct = disjunct;
            _logicSet = data;
        }
        private Disjunction<bool> _disjunct;
        private LogicSet _logicSet;
        private int _correct;
        private int _wrong;
        public float GetError()
        {
            
            return calculateError();
            
        }
        private float calculateError()
        {
            float error = 0.0f;
            foreach (List<bool> row in _logicSet.DataSet)
            {
                if (isCorrect(row))
                {
                    if (row[row.Count - 1])
                    {

                    }
                    else
                    {
                        error++;
                    }
                }
            }
            return error;
        }
        private bool isCorrect(List<bool> row)
        {
            bool anyConjunctsTrue = false;
            foreach (Conjunction<bool> con in _disjunct.Conjunctions)
            {
                if (isCorrect(row, con))
                {
                    return true;
                }
            }
            return anyConjunctsTrue;
        }
        private bool isCorrect(List<bool> row, Conjunction<bool> conjunct)
        {
            int featureIndex;
            int isTrueCount =0;
            foreach (IBooleanEntity<bool> b in conjunct.Conjuncts)
            {
                featureIndex = getFeatureNameIndex(b.FeatureName);
                if (featureIndex > -1)
                {
                    if (row.Count > featureIndex)
                    {
                        if (row[featureIndex])
                        {
                            isTrueCount++;
                        }
                    }
                }
                
            }
            if (isTrueCount == conjunct.Conjuncts.Count)
                return true;
            else
                return false;
            
        }
        private int getFeatureNameIndex(string featureName)
        {
            int i = 0;
            for(i=0;i<_logicSet.FeatureNames.Count; i++)
            {
                if (_logicSet.FeatureNames[i] == featureName)
                {
                    return i;
                }
            }
            return -1;
        }


    }
}
